﻿using System;
using System.Web.Services;
using System.Net.Mail;
using System.Configuration;

namespace thengine.com.automailer
{
    /// <summary>
    /// This web service sends email based upon
    /// a set of parameters passed in when calling
    /// the service.
    /// 
    /// Original Author:        Andy Carrington-Chappell
    /// Last Update:            24/06/2014
    /// Updated By:             Andy Carrington-Chappell
    /// Version:                1.1b
    /// </summary>
    [WebService(Namespace = "thengine.com.automailer")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)] 
    [System.Web.Script.Services.ScriptService]
    public class AutoMailer : System.Web.Services.WebService
    {

        [WebMethod]
        public void sendNotification(string user, string subject, string message)
        {
            message += @"\r\n\r\n*** Please do not reply to this message. This mailbox is not monitored and your mail will not be read. ***";
            
            try
            {
                string mailFrom = ConfigurationManager.AppSettings["from"];
                string mailTo = user + "@mydomaon.com"; /* Limit the scope of email sending */
                MailMessage mailObj = new MailMessage(mailFrom, mailTo, subject, message);
                SmtpClient SMTPServer = new SmtpClient(ConfigurationManager.ConnectionStrings["smtpserver"].ConnectionString);
                SMTPServer.Send(mailObj);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("AutoMailer", ex.Message, System.Diagnostics.EventLogEntryType.Error);
            }
        }
    }
}